﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-11>
//创建作者：<郭登杰>
//功能说明：来函流程公共控件-左侧来涵信息
//********************************************************************************//

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class; 

namespace Web.Controls.LAIHAN
{
    public partial class UCLHLCLAIHANXX : UserControl, IUCLHOperate
    {
        #region 属性

        public int FocusIndexGV
        {
            get { return Convert.ToInt32(ViewState["FocusIndexGV"] == null ? "-1" : ViewState["FocusIndexGV"]); }
            set { ViewState["FocusIndexGV"] = value; }
        }

        public int LHCount => gvList.VisibleRowCount;

        /// <summ
        ///     ary>
        ///     获取申请理由
        /// </summary>
        public string LaiHanNR
        {
            set { txtLaiHanContent.Text = value; }
            get { return txtLaiHanContent.Text; }
        }

        /// <summary>
        ///     设置来函内容启禁用
        /// </summary>
        /// <param name="enabled"></param>
        public void SetLaiHanNREnabled(bool enabled)
        {
            if (LaiHanLC == eLaiHanLCBH.业务员)
            {
                txtLaiHanContent.ReadOnly = !enabled;
                txtLaiHanContent.Enabled = enabled;
            }
        }

        /// <summary>
        ///     来函ID
        /// </summary>
        public int LaiHanID
        {
            get { return Convert.ToInt32(ViewState["LaiHanID"]); }
            set { ViewState["LaiHanID"] = value; }
        }

        /// <summary>
        ///     来函编号
        /// </summary>
        public string LaiHanBH
        {
            get { return Convert.ToString(ViewState["LaiHanBH"]); }
            set { ViewState["LaiHanBH"] = value; }
        }

        /// <summary>
        ///     来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        ///     来函流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC { get; set; }

        public DataTable DataSourceTOP
        {
            get { return ViewState["DataSourceTOP"] as DataTable; }
            set { ViewState["DataSourceTOP"] = value; }
        }

        public object LaiHanXXEntity
        {
            get { return ViewState["LaiHanXXEntity"] as object; }
            set { ViewState["LaiHanXXEntity"] = value; } 
        }

        public DataTable CommonXXDT
        {
            get { return ViewState["CommonXXDT"] as DataTable; }
            set { ViewState["CommonXXDT"] = value; }
        }


        #endregion

        #region 变量

        /// <summary>
        ///     数据库操作
        /// </summary>
        private readonly SpHelper _sphelper = new SpHelper();

        /// <summary>
        ///     年度调整BLL
        /// </summary>
        private readonly YS_LH_NIANTIAOZBLL _YS_LH_NIANTIAOZBLL = new YS_LH_NIANTIAOZBLL();

        /// <summary>
        ///     来函信息处理类
        /// </summary>
        private readonly YS_LH_LAIHANXXProcess _YS_LH_LAIHANXXBLL = new YS_LH_LAIHANXXProcess();

        /// <summary>
        ///     月度调整处理类
        /// </summary>
        private YS_LH_YUETIAOZProcess _YS_LH_YUETIAOZ = new YS_LH_YUETIAOZProcess();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                InitControl();
            //gvList.DataSource = DataSourceTOP;
            //gvList.DataBind();
        }

        /// <summary>
        ///     焦点变更事件
        /// </summary>
        protected void gvList_FocusedRowChanged(object sender, EventArgs e)
        {
            BindRight();
        }

        /// <summary>
        ///     响应前台选中项变化
        /// </summary>
        protected void btnPerformServer_Click(object sender, EventArgs e)
        {
            //BindRight();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化控件
        /// </summary>
        public void InitControl()
        {
            txtLaiHanContent.ReadOnly = true;
            txtYWYContent.ReadOnly = true;
            if (LaiHanLC == eLaiHanLCBH.业务员)
            {
                trYWYContent.Visible = false;
                txtLaiHanContent.Height = 190;
                txtLaiHanContent.ReadOnly = false;
                //this.lblLaiHanContentTip.Text = "申请理由";
            }
        }

        /// <summary>
        ///     已确认的来函显示背景为绿色，未确认的为默认颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvLH_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != GridViewRowType.Data) return;

            var s = Convert.ToInt32(e.GetValue("I_DANGQIANBZQR"));

            if (s > 0)

                e.Row.BackColor = Color.LightGreen;
        }

        /// <summary>
        ///     确认验证,仅业务员时需要
        /// </summary>
        public ValidateRS ValidateQR()
        {
            var rs = new ValidateRS();
            if (LaiHanLX != eLaiHanLX.计划指标注销)
                if (LaiHanLC == eLaiHanLCBH.业务员 && txtLaiHanContent.Text == "")
                {
                    rs.Msg = "请填写来函内容。";
                    return rs;
                }

            rs.IsSuccess = true;
            return rs;
        }
        /// <summary>
        ///     提交验证
        /// </summary>
        public ValidateRS Validate()
        {
            int iSLYM = int.Parse(BLL.BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()));  //系统水量年月
            var lhxx = _YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(decimal.Parse(LaiHanID.ToString()));  //用户申请水量年月
            var rs = new ValidateRS();
            var laihanList = SelectedLaiHanXX();
            if (laihanList.Count == 0)
            {
                rs.Msg = "请选择要提交的来函。";
                return rs;
            }
            string month = lhxx.I_TZM.ToString();
            if (month.Length <= 1)
            {
                month = "0" + month;
            }
            string ym = lhxx.I_TZY.ToString() + month;
            if(lhxx.I_LAIHANLX==1)
            {
                if (iSLYM != int.Parse(ym))
                {
                    rs.Msg = "年月不对。";
                    return rs;
                }
            }
           
            rs.IsSuccess = true;
            return rs;
        }

        /*
        /// <summary>
        /// 判断来函是否确认
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool IsConfirm(YS_LH_LAIHANXX item)
        {
            DataRowCollection drcTemp = this._sphelper.P_YS_LHGETISCONFIRM((int)item.I_LAIHANLX, item.S_LAIHANBH, (int)this.LaiHanLC).Tables[0].Rows;
            if (drcTemp.Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        */
        /// <summary>
        ///     清空信息
        /// </summary>
        public void ClearInfo()
        {
        }

        /// <summary>
        ///     根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            txtLaiHanContent.Text = laihanxx.S_SHENQINGLY;
            txtLaiHanContent.ReadOnly = !string.IsNullOrEmpty(laihanxx.S_SHENQINGLY);
            if (LaiHanLC == eLaiHanLCBH.业务员)
            {
                trYWYContent.Visible = false;
                txtLaiHanContent.Height = 190;
                txtLaiHanContent.ReadOnly = false;
            }
        }

        //绑定列表
        public void BindList(string guanlik, string laihanbh, string yonghuh, object ywy, object fushenxg)
        {
            int iFt = Convert.ToInt32(this.Request["ft"]);
            int iUserID = 1 == iFt ? 45 : LoginInfo.CurrentUser.UserID;
            FocusIndexGV = gvList.FocusedRowIndex;
            var table = BusinessTool.GetTextAndValue(
                "View_AS_UserInfo", "ROLENAME", "ROLEID", null, "userid=" + iUserID
            ).Tables[0];
            //var roleid = table.Rows[0]["VALUE"].ToString();
            if (LaiHanLC == eLaiHanLCBH.业务员)
            {
                foreach (DataRow row in table.Rows)
                {
                    if (row["VALUE"].ToString() == "9001")
                    {
                        ywy = iUserID;
                    }
                }
            }
            //if (roleid == "9001") ywy = LoginInfo.CurrentUser.UserID;
            //记录当前勾选的来函ID
            var listChecked = new Dictionary<string, string>();
            for (var i = 0; i < gvList.VisibleRowCount; i++)
            {
                var cb = (ASPxCheckBox) gvList.FindRowCellTemplateControl(i, (GridViewDataColumn) gvList.Columns[0],
                    "cid");
                if (cb.Checked)
                {
                    var hid = (HiddenField) gvList.FindRowCellTemplateControl(i, (GridViewDataColumn) gvList.Columns[0],
                        "id");
                    listChecked.Add(hid.Value, hid.Value);
                }
            }

            var zd = BusinessTool.WType != 0 || BusinessTool.ZHANDIANBH() == "00" ? null : BusinessTool.ZHANDIANBH();
            var dt = _sphelper.P_YS_LH_LAIHANCX_LC(iUserID, (int) LaiHanLC, zd, guanlik, laihanbh,
                yonghuh, (int) LaiHanLX, ywy == null ? null : (int?) Convert.ToInt32(ywy),
                fushenxg == null ? null : (int?) Convert.ToInt32(fushenxg)).Tables[0];
            this.DataSourceTOP = dt;
            gvList.DataSource = DataSourceTOP;
            gvList.DataBind();
            if (dt.Rows.Count > 0)
            {
                gvList.FocusedRowIndex = -1;
                gvList.FocusedRowIndex = 0;
                var vid = 0;
                if (int.TryParse(Request["ID"], out vid))
                    for (var i = 0; i < gvList.VisibleRowCount; i++)
                    {
                        var hid = (HiddenField) gvList.FindRowCellTemplateControl(i,
                            (GridViewDataColumn) gvList.Columns[0], "id");
                        if (Convert.ToInt32(hid.Value) == vid)
                        {
                            gvList.FocusedRowIndex = i;
                            break;
                        }
                    }

                //勾选之前勾选过的
                for (var i = 0; i < gvList.VisibleRowCount; i++)
                {
                    var hid = (HiddenField) gvList.FindRowCellTemplateControl(i, (GridViewDataColumn) gvList.Columns[0],
                        "id");
                    if (listChecked.ContainsKey(hid.Value))
                        ((ASPxCheckBox) gvList.FindRowCellTemplateControl(i, (GridViewDataColumn) gvList.Columns[0],
                            "cid")).Checked = true;
                }
            }
            else
            {
                txtLaiHanContent.Text = "";
                txtYWYContent.Text = "";
                LaiHanLCTools.ClearRightInfo(Parent);
            }

            if (gvList.VisibleRowCount > FocusIndexGV && FocusIndexGV > -1) gvList.FocusedRowIndex = FocusIndexGV;
        }

        /// <summary>
        ///     设置列表中来函是否勾选
        /// </summary>
        /// <param name="check">true:勾选</param>
        public void SetListChecked(bool check)
        {
            for (var i = 0; i < gvList.VisibleRowCount; i++)
            {
                var cb = (ASPxCheckBox) gvList.FindRowCellTemplateControl(i, (GridViewDataColumn) gvList.Columns[0],
                    "cid");
                cb.Checked = check;
            }
        }

        /// <summary>
        ///     绑定来函详细信息
        /// </summary>
        private void BindRight()
        {
            //记录当前选择项ID
            var id = gvList.GetRowValues(gvList.FocusedRowIndex, "ID");
            if (id == null)
            {
                LaiHanID = -1;
                return;
            }

            LaiHanID = Convert.ToInt32(id); 
            if (null == LaiHanXXEntity)
            {
                LaiHanXXEntity= new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(LaiHanID);
            }
            YS_LH_LAIHANXX laihanxx = LaiHanXXEntity as YS_LH_LAIHANXX; 
            LaiHanBH = Convert.ToString(gvList.GetRowValues(gvList.FocusedRowIndex, "S_LAIHANBH"));
            LoadInfo(laihanxx);
            //绑定业务员意见
            if (laihanxx.I_LIUCHENGBH >= (int) eLaiHanLCBH.内勤)
            {
                var drc = new DataTable().Rows;
                var condition = "S_LAIHANBH='" + laihanxx.S_LAIHANBH + "' AND I_LIUCHENGBH=" + (int) eLaiHanLCBH.业务员;
                if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.年度)
                    drc = _YS_LH_NIANTIAOZBLL.SelectYS_LH_NIANTIAOZsDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.基建调整)
                    drc = new YS_LH_JIJIANTZBLL().SelectYS_LH_JIJIANTZsDynamic(
                        "S_LAIHANBH='" + laihanxx.S_LAIHANBH + "' AND I_Y=" + laihanxx.I_TZY + " AND I_LIUCHENGBH=" +
                        (int) eLaiHanLCBH.业务员, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.月度)
                {
                    //if (null == CommonXXDT)
                    //{
                        CommonXXDT = new YS_LH_YUETIAOZProcess()
                       .SelectYS_LH_YUETIAOZsDynamic("I_JLZT=0 AND S_LAIHANBH='" + laihanxx.S_LAIHANBH + "'", null);
                    //}
                    drc = CommonXXDT.Rows;
                }
               
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划变更)
                    drc = new YS_LH_JIHUABGBLL().SelectYS_LH_JIHUABGsDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.表计因素注销)
                    drc = new YS_LH_BIAOJIYSZXProcess().SelectYS_LH_BIAOJIYSZXesDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.计划指标注销)
                    drc = new YS_LH_JIHUAZBZXProcess().SelectYS_LH_JIHUAZBZXesDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.分户)
                    drc = new YS_LH_FENHUXXBLL().SelectYS_LH_FENHUXXesDynamic(
                        "S_LAIHANBH='" + laihanxx.S_LAIHANBH + "' AND I_JLZT=0 and S_YONGHUHFATHER='" +
                        laihanxx.S_YONGHUBH + "' and rownum=1 ", null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.并户)
                    drc = new YS_LH_BINGHUBLL().SelectYS_LH_BINGHUsDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.销户)
                    drc = new YS_LH_XIAOHUBLL().SelectYS_LH_XIAOHUsDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.销表)
                    drc = new YS_LH_XIAOBIAOBLL().SelectYS_LH_XIAOBIAOsDynamic(condition, null).Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.退款)
                    drc = new YS_LH_TUIKUANBLL().SelectYS_LH_TUIKUANsDynamic(condition + "  AND I_DANGQIAN=1", null)
                        .Rows;
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.改户)
                    drc = new YS_LH_GAIHUXXBLL().SelectYS_LH_GAIHUXXesDynamic(condition, null).Rows;
                else if ((int) LaiHanLX >= 20)
                    drc = new QS_LH_TONGYONGBZYJProcess().SelectQS_LH_TONGYONGBZYJsDynamic(condition, null).Rows;
                if (drc.Count != 1)
                {
                    throw new Exception("该来函流程无业务员记录或业务员记录数不正确,请检查");
                }

                if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.月度)
                    txtYWYContent.Text = drc[0]["S_YEWUYJY"].ToString();
                else if (laihanxx.I_LAIHANLX == (int) eLaiHanLX.分户)
                    txtYWYContent.Text = drc[0]["S_YEWUY"].ToString();
                else
                    txtYWYContent.Text = drc[0]["S_SHENPIYJ"].ToString();
            }

            LaiHanLCTools.LoadRightInfo(Parent, laihanxx);
            LaiHanXXEntity = null;
        }

        /// <summary>
        ///     获取所有选中的来涵信息
        /// </summary>
        /// <returns></returns>
        public List<YS_LH_LAIHANXX> SelectedLaiHanXX()
        {
            var list = new List<YS_LH_LAIHANXX>();
            var objT = new YS_LH_LAIHANXX();
            if (gvList.VisibleRowCount > 1)
            {
                for (var i = 0; i < gvList.VisibleRowCount; i++)
                {
                    var ck = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0],
                        "cid");
                    var hid = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0],
                        "id");
                    if (ck.Checked)
                    {
                        objT = _YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(Convert.ToDecimal(hid.Value));
                        list.Add(objT);
                    }
                }
            }
            else
            {
                var hid = (HiddenField)gvList.FindRowCellTemplateControl(0, (GridViewDataColumn)gvList.Columns[0],
                    "id");
                objT = _YS_LH_LAIHANXXBLL.SelectYS_LH_LAIHANXX(Convert.ToDecimal(hid.Value));
                list.Add(objT);
            }

            return list;
        }

        #endregion
    }

  
}